/**
 * Created by lixiang01 on 2/10/2017.
 */
var picker =null;

(function($$) {
    $$.init();


    var reportData = {
        reportInfos: []
    };

    var reportVue = new Vue({
        el: '#divController',

        data: function () {
            return reportData;
        },
        methods: {
            chooseTime: function (event) {


                var d = new Date();
                var month = d.getMonth()+1;
                var day = d.getDate();
                if(month<10){
                    month = "0"+month;
                }
                if(day<10){
                    day ="0"+day;
                }
                var nowDate = d.getFullYear()+"-"+month+"-"+day;

                var target = event.currentTarget;
                var  optionsJson='{"type":"date","value":"'+nowDate+'","beginYear":2010,"endYear":2020}';
                var ops = '{"type":"date","value":"2015-03-10","beginYear":2010,"endYear":2020}';
                var options = JSON.parse(optionsJson);
                var id = target.getAttribute('id');

                picker = new $$.DtPicker(options);
                picker.show(function(rs) {
                    var timeStamp = new Date(rs.toString()).getTime()/1000;
                    getReportData(timeStamp);
                    picker.dispose();
                });
            },
            editReport:function (event,param) {
                var target = event.currentTarget;

                var dataId = target.getAttribute("data-id");

                var newHtml = "reportEdit.html?id="+dataId;
                window.location.replace(newHtml);

            }

        }

    });


    var timStamp = Date.parse(new Date()) / 1000;
    getReportData(timStamp);


    function getReportData(timeStamp) {

        Vue.http.get(webRoot + "/report/getReport/" + timeStamp, {credentials: true}).then(function (response) {
                var datas = response.body;
                if (datas['error'] == 0) {
                    reportData.reportInfos = datas['object'];
                    getTotalPrice();
                } else {
                    alert(datas['message'])
                }

            },
            function (response) {
                // error callback
            });

    }

    function getTotalPrice() {
        var total = 0;
        var mount = 0;
        for (var i = 0; i < reportData.reportInfos.length; i++) {
            total += reportData.reportInfos[i].salePrice;
            mount += reportData.reportInfos[i].saleNum;
        }
        var words = mount + "条," + total + "元";
        document.getElementById("totalAmount").innerHTML=words;
        return total;
    }
})(mui);






